package org.app.original;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import org.app.MyUtils;

public class MyImage {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String path="img/穷奇.jpg";
		new DemoPanel().show(new MyImage().getImg(path));
		System.out.println("^_^");
		MyImage my=new MyImage();
		my.draw();
		System.out.println("**");
	}
	
	
	
	public JPanel getImg(String path){
		final File input=new File("src/main/resources/"+path);
		System.out.println(input.getAbsolutePath());
		return new JPanel(){

			BufferedImage img;
			{
				try {
				img=ImageIO.read(input);
				int w=img.getWidth(null);
				int h=img.getHeight(null);
				this.setPreferredSize(new Dimension(w,h));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
			
			
			public void paintComponent(Graphics g) {
				// TODO Auto-generated method stub
				super.paintComponent(g);

				
					
				//	g.drawImage(img, 0, 0, this);
						
				Graphics2D g2d=(Graphics2D)g;
				img=toFlig(img);
			//	img=toReflection(img);
				g2d.drawImage(img, null,0,0);
				save(img,"翻转.jpg");
			}
			
		};
	
	}
	
	/**
	 * 翻转
	 * @param img
	 * @return
	 */
	public BufferedImage toFlig(BufferedImage img){
		int w=img.getWidth(null);
		int h=img.getHeight();
		//BufferedImage.TYPE_BYTE_GRAY. 灰度
		BufferedImage bImg=new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB);
		


		AffineTransform tx=AffineTransform.getScaleInstance(-1,1);
		tx.translate(-w,0);
		
		AffineTransformOp op=new AffineTransformOp(tx,AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
		bImg=op.filter(img, null);
		return bImg;
	}
//	
//	public BufferedImage toReflection(BufferedImage img){
//		int gap = 20;
//        float opacity = 0.4f;
//        float fadeHeight = 0.3f;
//		int w=img.getWidth(null);
//		int h=img.getHeight();
//		//BufferedImage.TYPE_BYTE_GRAY. 灰度
//		BufferedImage bImg=new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB);
//		Graphics2D g2d = bImg.createGraphics();
//		g2d.drawRenderedImage(img, null);
//        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN));
//        g2d.setPaint(new GradientPaint(0, h * fadeHeight,
//                                      new Color(0.0f, 0.0f, 0.0f, 0.0f), 0,
//                                      h,
//                                      new Color(0.0f, 0.0f, 0.0f, opacity)));
//
//        g2d.fillRect(0, 0, w, h);
//        g2d.translate(0, 2 * h + gap);
//        g2d.scale(1, -1);
//        g2d.dispose();
//		return bImg;
//	}
	
	
	/**
	 * 
	 * 更加文件名保存图片（带后缀）
	 * @param bImg
	 * @param path
	 */
	public void save(BufferedImage bImg,String path){
		String format="";
		try {
	
			File file=new File(path);
			System.out.println(file.getAbsolutePath());
			if(!file.exists()){
				file.createNewFile();
			}
			String name=file.getName();
			int index=name.lastIndexOf('.');
			format=name.substring(index+1);
			System.out.println(format);
			ImageIO.write(bImg, format,file );
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void draw(){
		int row=3;
		int col=3;
		int [][]matrix=new int[row][col];
		for(int i=0;i<row;i++){
			matrix[i][1]=matrix[1][i]=1;
		}
		
		MyUtils.show(matrix);
		int unit=36;
		
		BufferedImage img=new BufferedImage((2*row-1)*unit,(2*row-1)*unit,BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d=img.createGraphics();
		BasicStroke bs1 = new BasicStroke(8, BasicStroke.CAP_ROUND, 
	            BasicStroke.JOIN_BEVEL);
	        g2d.setStroke(bs1);
		for(int i=0;i<row;i++){
			
			
			for(int j=0;j<col;j++){
			
				if(matrix[i][j]!=0){
					int x=(2*i)*unit;
					int y=(2*j)*unit;
					int wide=unit;
					int high=unit;
					g2d.drawRect(x, y,wide, high);
				}
			}
		}
		
		String path="rect.jpg";
		this.save(img, path);
	}
	
	/**
	 * 画一方框，在方框中间显示文字
	 * @param g2d
	 * @param temp
	 * @param unit
	 */
	public void drawStringWithBorder(Graphics2D g2d,String temp,int x,int y,int unit){
		int size=g2d.getFont().getSize();
		int sum=temp.length();//文字个数
		int length=size*sum;
		while(unit<length){
			size=size*2/3;
			length=size*sum;
		//	g2d.getFont().;
		}
		g2d.drawRect(x, y, unit,unit);
		g2d.drawString(temp, x+(unit-length)/2, y+(unit-size)/2);

	}
	

}
